﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Collections;
using System.IO;
using System.Threading;
using System.Runtime.InteropServices;
using DeviceSDK.LED;
using BaseHelper;
using DeviceSDK;
using BaseHelper.Base;

namespace DeviceSDK.LED.OnBon
{
    /// <summary>
    ///仰邦接收卡
    /// </summary>
    public class OnBon_YQ
    {
        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
        private delegate void SendProgramHandler();



        private static bool IsInit = false;
        private static object locker = new object();
         

        public static string Get_Error(int err)
        {
            return LedYQNetSdk.GetError(err);
        }

        //public static int Get_Status(string IP)
        //{
        //    //2019.6.24 
        //    //LedYQNetSdk.Init();
        //    Init();
        //    short w = 0;
        //    short h = 0;
        //    ushort type = 0;
        //    int err=  LedYQNetSdk.Net_GetScreeninfo(IP, ref type, ref w, ref h);
        //    //LedYQNetSdk.Release();
        //    return err;
        //}


        public static void Init()
        {
            lock (locker)
            {
                if (!IsInit)
                {
                    LedYQNetSdk.Init();
                    Application.DoEvents();
                    IsInit = true;
                }
            }
        }

        //发送-节目
        public int Send_Programs(string IP, PlaybillProgramInfo[] Programs)
        {
            int ret = 0;
            try
            {
               
                lock (locker)
                {
                    Init();
                    LedNetSdkStruct.Program[] programs;//节目数组

                    List<LedNetSdkStruct.Program> ltProgram = new List<LedNetSdkStruct.Program>();//声明节目列表
                    List<LedNetSdkStruct.PicArea> ltPicArea = new List<LedNetSdkStruct.PicArea>();//声明区域列表
                    List<LedNetSdkStruct.ImgText> ltImgTxt = new List<LedNetSdkStruct.ImgText>();//声明图文列表
                    List<LedNetSdkStruct.Video> ltVideo = new List<LedNetSdkStruct.Video>();//视频列表

                    foreach (PlaybillProgramInfo pinfo in Programs)
                    {
                        #region foreach

                        LedNetSdkStruct.Program program = new LedNetSdkStruct.Program();//声明单个节目
                        LedNetSdkStruct.PicArea picArea = new LedNetSdkStruct.PicArea();//声明单个区域

                        pinfo.IsEffectRand = pinfo.IsEffectRand == 0 ? 0 : 1;

                        program.m_w = (short)pinfo.Width;//节目的宽度  现在直接设为灯屏的宽度
                        program.m_h = (short)pinfo.Height;//节目的高度 现在直接设为灯屏的高度
                        program.m_play_mode = 1;//节目播放方式
                        program.m_play_time = 1;//节目播放时长
                        program.m_bDate = false;//是否设置节目播放日期
                        program.m_aging_start_time = new byte[0];//节目开始播放日期
                        program.m_aging_stop_time = new byte[0];//节目播放结束日期
                        program.m_bTime = false;//是否设置节目播放时间
                        program.m_period_ontime = new byte[0];//节目开始播放时间
                        program.m_period_offtime = new byte[0];//节目结束播放时间
                        program.m_play_week = 127;// (byte)System.Convert.ToInt32(str_week, 2);//设置节目播放的星期几



                        picArea.m_x = (short)pinfo.X;//区域X轴坐标
                        picArea.m_y = (short)pinfo.Y;//区域Y轴坐标
                        picArea.m_w = (short)pinfo.Width;//区域宽度
                        picArea.m_h = (short)pinfo.Height;//区域高度
                        picArea.m_bBgTransparent = 100;//区域窗口透明度
                        picArea.m_transparency = false;//区域背景是否透明
                        picArea.m_window_type = 1;//区域窗口类型


                        int FileType = ToolsHelper.GetFileType(pinfo.FilePath);

                        if (FileType == 0)
                        {
                            #region  图片
                            LedNetSdkStruct.ImgText imgTxt = new LedNetSdkStruct.ImgText();//声明单个图文
                            picArea.thing = 0;////区域类型0图文1视频2字幕3时间4表盘5农历6计时


                            imgTxt.bPic = true;//true表示当前显示的是图片 false表示是文档
                            imgTxt.FileName = pinfo.FilePath;//图文文件全路径信息
                            imgTxt.szFile = System.Text.Encoding.Unicode.GetBytes(pinfo.FilePath);//文件名
                            imgTxt.display_effects = (byte)pinfo.IsEffectRand; //2;//指定特效效果
                            imgTxt.display_speed = 1;// (byte)display_speed;//图片运行时间  从无到有的时长 单位秒
                            imgTxt.stay_time = (ushort)pinfo.PlayTime;// (ushort)stay_time;//图片在大屏上的停留时间 单位秒



                            ltImgTxt.Add(imgTxt);//将图文信息添加到图文列表中
                            picArea.m_ImgText = ltImgTxt.ToArray();//区域添加图文列表信息
                            ltPicArea.Add(picArea);//区域列表添加区域
                            program.m_PicArea = ltPicArea.ToArray();//节目添加区域列表
                            #endregion
                        }

                        else if (FileType == 1)
                        {
                            #region 视频
                            LedNetSdkStruct.Video videoes = new LedNetSdkStruct.Video();
                            picArea.thing = 1;////区域类型0图文1视频2字幕3时间4表盘5农历6计时


                            videoes.FileName = pinfo.FilePath;//视频路径
                            videoes.szFile = System.Text.Encoding.Unicode.GetBytes(pinfo.FilePath);//视频名称
                            videoes.scale_mode = (byte)1;//缩放模式
                            videoes.volume = (byte)100;//音量


                            ltVideo.Add(videoes);//将图文信息添加到图文列表中
                            picArea.m_Video = ltVideo.ToArray();//区域添加图文列表信息
                            ltPicArea.Add(picArea);//区域列表添加区域
                            program.m_PicArea = ltPicArea.ToArray();//节目添加区域列表
                            #endregion
                        }

                        else if (FileType == 2)
                        {
                            #region 文字
                            LedNetSdkStruct.ImgText imgTxt = new LedNetSdkStruct.ImgText();//声明单个图文
                            picArea.thing = 0;////区域类型0图文1视频2字幕3时间4表盘5农历6计时


                            imgTxt.bPic = false;//true表示当前显示的是图片 false表示是文档
                            imgTxt.FileName = pinfo.FilePath;//图文文件全路径信息
                            imgTxt.szFile = System.Text.Encoding.Unicode.GetBytes(pinfo.FilePath);//文件名
                            imgTxt.display_effects = (byte)pinfo.IsEffectRand; //2;//指定特效效果
                            imgTxt.display_speed = 1;// (byte)display_speed;//图片运行时间  从无到有的时长 单位秒
                            imgTxt.stay_time = (ushort)pinfo.PlayTime;// (ushort)stay_time;//图片在大屏上的停留时间 单位秒


                            ltImgTxt.Add(imgTxt);//将图文信息添加到图文列表中
                            picArea.m_ImgText = ltImgTxt.ToArray();//区域添加图文列表信息
                            ltPicArea.Add(picArea);//区域列表添加区域
                            program.m_PicArea = ltPicArea.ToArray();//节目添加区域列表
                            #endregion
                        }
                        #endregion
                        ltProgram.Add(program);//节目列表添加节目

                    }


                    programs = ltProgram.ToArray();//节目列表转为节目数组

                    #region 创建节目单
                    //列表
                    uint hPlaylist = LedProgramSdk.YQ_CreatePlaylist();
                    for (int a = 0; a < programs.Length; a++)
                    {
                        //节目
                        uint hProgram = LedProgramSdk.YQ_CreateProgram(programs[a].m_w, programs[a].m_h);
                        for (int b = 0; b < programs[a].m_PicArea.Length; b++)
                        {
                            #region proGram
                            //区域类型0图文1视频2字幕3时间4表盘5农历6计时
                            switch (programs[a].m_PicArea[b].thing)
                            {
                                case 0:
                                    {
                                        LedNetSdkStruct.PicArea picArea = programs[a].m_PicArea[b];
                                        uint hArea = LedProgramSdk.YQ_CreatePicArea(picArea.m_x, picArea.m_y, picArea.m_w, picArea.m_h,
                                                picArea.m_bBgTransparent, picArea.m_window_type, picArea.m_transparency);
                                        for (int i = 0; i < picArea.m_ImgText.Length; i++)
                                        {
                                            if (picArea.m_ImgText[i].bPic == true)
                                            {
                                                LedProgramSdk.YQ_PicAreaAddImageUnit(hArea, picArea.m_ImgText[i].szFile, picArea.m_ImgText[i].display_effects, picArea.m_ImgText[i].display_speed, picArea.m_ImgText[i].stay_time);
                                            }
                                            else
                                            {
                                                LedProgramSdk.YQ_PicAreaAddRtfUnit(hArea, picArea.m_ImgText[i].szFile, picArea.m_ImgText[i].display_effects, picArea.m_ImgText[i].display_speed, picArea.m_ImgText[i].stay_time);
                                            }
                                        }
                                        LedProgramSdk.YQ_ProgramAddArea(hProgram, hArea);
                                        break;
                                    }
                                case 1:
                                    {
                                        LedNetSdkStruct.PicArea picArea = programs[a].m_PicArea[b];
                                        uint hArea = LedProgramSdk.YQ_CreateVideoArea(picArea.m_x, picArea.m_y, picArea.m_w, picArea.m_h, picArea.m_bBgTransparent);
                                        for (int c = 0; c < picArea.m_Video.Length; c++)
                                        {
                                            LedProgramSdk.YQ_VideoAreaAddUnit(hArea, picArea.m_Video[c].szFile, picArea.m_Video[c].scale_mode, picArea.m_Video[c].volume);
                                        }
                                        LedProgramSdk.YQ_ProgramAddArea(hProgram, hArea);
                                        break;
                                    }
                                case 2:
                                    {
                                        LedNetSdkStruct.PicArea picArea = programs[a].m_PicArea[b];
                                        uint hArea = LedProgramSdk.YQ_CreatePicArea(picArea.m_x, picArea.m_y, picArea.m_w, picArea.m_h,
                                                picArea.m_bBgTransparent, picArea.m_window_type, picArea.m_transparency);
                                        for (int i = 0; i < picArea.m_ImgText.Length; i++)
                                        {
                                            LedProgramSdk.YQ_PicAreaAddRtfUnit(hArea, picArea.m_ImgText[i].szFile, picArea.m_ImgText[i].display_effects, picArea.m_ImgText[i].display_speed, picArea.m_ImgText[i].stay_time);
                                        }
                                        LedProgramSdk.YQ_ProgramAddArea(hProgram, hArea);
                                        break;
                                    }
                                case 3:
                                    {
                                        LedNetSdkStruct.PicArea picArea = programs[a].m_PicArea[b];
                                        uint hArea = LedProgramSdk.YQ_CreateCNTimeArea(picArea.m_x, picArea.m_y, picArea.m_w, picArea.m_h, picArea.m_bBgTransparent,
                                                picArea.m_Time_Area.timediff_flag, picArea.m_Time_Area.timediff_hour, picArea.m_Time_Area.timediff_min,
                                                picArea.m_Time_Area.font, picArea.m_Time_Area.fontsize, picArea.m_Time_Area.bold, picArea.m_Time_Area.italic, picArea.m_Time_Area.underline,
                                                picArea.m_Time_Area.align, picArea.m_Time_Area.multiline,
                                                picArea.m_Time_Area.day_enable, picArea.m_Time_Area.daycolor,
                                                picArea.m_Time_Area.week_enable, picArea.m_Time_Area.weekcolor,
                                                picArea.m_Time_Area.time_enable, picArea.m_Time_Area.timecolor,
                                                picArea.m_Time_Area.text_enable, picArea.m_Time_Area.textcolor, picArea.m_Time_Area.statictext);
                                        LedProgramSdk.YQ_ProgramAddArea(hProgram, hArea);
                                        break;
                                    }
                                case 4:
                                    {
                                        LedNetSdkStruct.PicArea picArea = programs[a].m_PicArea[b];
                                        uint hArea = LedProgramSdk.YQ_CreateClockStyleArea(picArea.m_x, picArea.m_y, picArea.m_w, picArea.m_h, picArea.m_bBgTransparent,
                                            picArea.m_Clock_Area.text_enable, picArea.m_Clock_Area.textcolor, picArea.m_Clock_Area.statictex,
                                            picArea.m_Clock_Area.text_font, picArea.m_Clock_Area.text_fontsize, picArea.m_Clock_Area.text_bold, picArea.m_Clock_Area.text_italic, picArea.m_Clock_Area.text_underline,
                                            picArea.m_Clock_Area.ymd_enable, picArea.m_Clock_Area.ymdcolor,
                                            picArea.m_Clock_Area.week_enable, picArea.m_Clock_Area.weekcolor,
                                            picArea.m_Clock_Area.hourhand_color, picArea.m_Clock_Area.minhand_color, picArea.m_Clock_Area.secondhand_color,
                                            picArea.m_Clock_Area.timediff_flag, picArea.m_Clock_Area.timediff_hour, picArea.m_Clock_Area.timediff_min,
                                            picArea.m_Clock_Area.rightangle_color, picArea.m_Clock_Area.integral_color, picArea.m_Clock_Area.minute_color, picArea.m_Clock_Area.dwClockFormat);
                                        LedProgramSdk.YQ_ProgramAddArea(hProgram, hArea);
                                        break;
                                    }
                                case 5:
                                    {
                                        LedNetSdkStruct.PicArea picArea = programs[a].m_PicArea[b];
                                        uint hArea = LedProgramSdk.YQ_CreateLunarArea(picArea.m_x, picArea.m_y, picArea.m_w, picArea.m_h, picArea.m_bBgTransparent,
                                        picArea.m_Lun_Area.text_font, picArea.m_Lun_Area.text_fontsize, picArea.m_Lun_Area.text_bold, picArea.m_Lun_Area.text_italic, picArea.m_Lun_Area.text_underline,
                                        picArea.m_Lun_Area.align, picArea.m_Lun_Area.multiline, picArea.m_Lun_Area.year_enable, picArea.m_Lun_Area.yearcolor,
                                        picArea.m_Lun_Area.day_enable, picArea.m_Lun_Area.daycolor, picArea.m_Lun_Area.solarterms_enable, picArea.m_Lun_Area.solartermscolor,
                                        picArea.m_Lun_Area.text_enable, picArea.m_Lun_Area.textcolor, picArea.m_Lun_Area.statictext);
                                        LedProgramSdk.YQ_ProgramAddArea(hProgram, hArea);
                                        break;
                                    }
                                case 6:
                                    {
                                        LedNetSdkStruct.PicArea picArea = programs[a].m_PicArea[b];
                                        uint hArea = LedProgramSdk.YQ_CreateTimeCounterArea(picArea.m_x, picArea.m_y, picArea.m_w, picArea.m_h, picArea.m_bBgTransparent,
                                        picArea.m_Counter_Area.font, picArea.m_Counter_Area.fontsize, picArea.m_Counter_Area.bold, picArea.m_Counter_Area.italic, picArea.m_Counter_Area.underline,
                                        picArea.m_Counter_Area.align, picArea.m_Counter_Area.multiline,
                                        picArea.m_Counter_Area.target_date, picArea.m_Counter_Area.target_time, picArea.m_Counter_Area.bTimeFlag, picArea.m_Counter_Area.counter_color,
                                        picArea.m_Counter_Area.day_enable, picArea.m_Counter_Area.daytext, picArea.m_Counter_Area.hour_enable, picArea.m_Counter_Area.hourtext,
                                        picArea.m_Counter_Area.min_enable, picArea.m_Counter_Area.minutetext, picArea.m_Counter_Area.sec_enable, picArea.m_Counter_Area.secondtext,
                                        picArea.m_Counter_Area.add_enable, picArea.m_Counter_Area.unit_enable,
                                        picArea.m_Counter_Area.text_enable, picArea.m_Counter_Area.textcolor, picArea.m_Counter_Area.statictext);
                                        LedProgramSdk.YQ_ProgramAddArea(hProgram, hArea);
                                        break;
                                    }
                            }
                            #endregion
                        }
                        LedProgramSdk.YQ_PlaylistAddProgram(hPlaylist, hProgram, programs[a].m_play_mode, programs[a].m_play_time,
                                    programs[a].m_aging_start_time, programs[a].m_aging_stop_time, programs[a].m_period_ontime, programs[a].m_period_offtime, programs[a].m_play_week);

                    }
                    #endregion


                    string tempFolder = LEDHelper.TempFullPath +IP.Replace(".", "_") + "\\";
                    if (!Directory.Exists(tempFolder))
                        Directory.CreateDirectory(tempFolder);

                    byte[] szLocalTempDir = System.Text.Encoding.Unicode.GetBytes(tempFolder);
                    try
                    {
                        LedYQNetSdk.Net_SendPrograms(IP, (byte)3, hPlaylist, szLocalTempDir, ref ret);
                        Thread th = new Thread(new ParameterizedThreadStart(DestoryPlaylist));
                        th.Name = "DeviceSDK.LED.OnBon.OnBon_YQ2A.Send_Programs().th.DestoryPlaylist()";
                        //th.IsBackground = true;
                        th.Start(tempFolder);
                        ThreadHelper.Add(th.ManagedThreadId, th.Name);
                       // LedYQNetSdk.Sdk_Release();
                    }
                    catch (Exception ex2)
                    {
                        LogHelper.Err("DeviceSDK.LED.OnBon.OnBon_YQ.cs", "SendProgram()->DestoryPlaylist(" + IP + ")", ex2);
                    }
                }//locker

            }
            catch (Exception ex)
            {
                LogHelper.Err("DeviceSDK.LED.OnBon.OnBon_YQ.cs", "SendProgram(" + IP + ")", ex);
            }

            return ret;
        }


        //节目-销毁
        private void DestoryPlaylist(object obj)
        {
            string tempPath = (string)obj;
            Thread.Sleep(10 * 1000);
            try
            {
                if (Directory.Exists(tempPath))
                    Directory.Delete(tempPath,true);
            }
            catch(Exception ex)
            {
                //LogHelper.Err("Service.Release.Device.LED.OnBon.OnBon_YQ.cs", "DestoryPlaylist()", ex);
            }
            ThreadHelper.Remove(Thread.CurrentThread.ManagedThreadId);            
        }
        


        #region 设置
        //重启
        public int Set_Reboot(string IP)
        {
            Init();
            //LedYQNetSdk.Init();
            int ret = LedYQNetSdk.Net_SetClientMode(Encoding.Unicode.GetBytes(IP));
            //LedYQNetSdk.Release();
            return ret;
        }

        //获得-屏幕状态
        public int Get_Screen_Status(string IP,ref int Value)
        {
            Init();
            Byte onoff = (Byte)0;
            //LedYQNetSdk.Init();
            int ret=LedYQNetSdk.Net_GetOnoff(IP, ref onoff);
            //LedYQNetSdk.Release();
            Value = (int)onoff;
            return ret;
        }

        //设置-屏幕-关闭
        public int Set_Screen_Close(string IP)
        {
            Init();
            ///LedYQNetSdk.Init();
            int ret= LedYQNetSdk.Net_CloseScreen(IP);
            //LedYQNetSdk.Release();

            return ret;
        }

        //设置-屏幕-开启
        public int Set_Screen_Open(string IP)
        {
            Init();
            //LedYQNetSdk.Init();
            int ret= LedYQNetSdk.Net_OpenScreen(IP);
            //LedYQNetSdk.Release();
            return ret;
        }


        //获得-屏幕亮度
        public int Get_Brightness(string IP, ref int Value)
        {
            byte mode = (byte)0;
            byte[] values = new byte[48];
            Init();
            //LedYQNetSdk.Init();
            int ret = LedYQNetSdk.Net_GetBrightness(IP, ref mode, values);
            Value =(int)values[0];
            //LedYQNetSdk.Release();
            return ret;
        }

        //设置-屏幕亮度
        public int Set_Brightness(string IP,int Value)
        {
            byte mode = (byte)0;
            short[] values = new short[48];
            values[0] =(short)Value;
            Init();
            //LedYQNetSdk.Init();
            int ret = LedYQNetSdk.Net_AdjustBrightness(IP,mode, values);
            //LedYQNetSdk.Release();
            return ret;
        }

        //获得-音量
        public int Get_Volume(string IP, ref int Value)
        {
            Init();
            //LedYQNetSdk.Init();
            byte value=new byte();
            int ret = LedYQNetSdk.Net_GetVolume(IP, ref value );
            Value = (int)value;
            //LedYQNetSdk.Release();
            return ret;
        }

        //设置-音量
        public int Set_Volume(string IP, int Value)
        {
            Init();
            //LedYQNetSdk.Init();
            int ret = LedYQNetSdk.Net_SetVolume(IP, (byte)Value);
            //LedYQNetSdk.Release();
            return ret;
        }

        //设置-时间
        public int Set_Time(string IP)
        {
            Init();
            //LedYQNetSdk.Init();
            int ret = LedYQNetSdk.Net_TimeCorrect(IP);
            //LedYQNetSdk.Release();
            return ret;

        }
        #endregion



    }
}
